package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.SiteDao;
import com.hrbxlh.util.pageUtil;
/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：SiteActionImpl.java
* Description：上课地点actionImpl类
* History：
* Date           Author               Desc
* 2017.04.11	  xzx                  建立	
* -------------------------------------------------------
*/
@Component("siteActionImpl")
@Transactional
public class SiteActionImpl {

	@Resource(name = "siteDao")
	private SiteDao siteDao;
	
	@Resource(name = "departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;
	
	/**
	 * @description 添加地点
	 * @param Map<String,Object> paramMap  地点名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> addSite(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		String failMsg = "";
		try {
			List<Map<String, Object>> siteList = siteDao.getSiteByName(paramMap);
			//判断地点名重复
			if(siteList != null && siteList.size() != 0){
				failMsg = "该地点名已存在";
			}else{
				//添加地点
				siteDao.addSite(paramMap);
				result = true;
			}
			if(!result){
				resultMap.put("failMsg", failMsg);
			}
			resultMap.put("result", result);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 修改地点
	 * @param Map<String,Object> paramMap  地点名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> saveSite(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		String failMsg = "";
		try {
			List<Map<String, Object>> siteList = siteDao.getSiteByName(paramMap);
			//判断地点名重复
			if(siteList != null && siteList.size() != 0){
				failMsg = "该地点名已存在";
			}else{
				//修改地点
				siteDao.saveSite(paramMap);
				result = true;
			}
			if(!result){
				resultMap.put("failMsg", failMsg);
			}
			resultMap.put("result", result);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 删除地点
	 * @param Map<String,Object> paramMap
	 * @return Map result存成功失败标识   failMsg存错误信息
	 * @throws Exception
	 */
	public Map<String, Object> delSite(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			String[] tables = {"relation","relsiteprofession"};	
			String[] errMsg = {"课表","专业"};	
			Map<String, Object> resultMap = departMentActionImpl.getDepartmentDelMark("地点",tables, errMsg, "siteId", (Integer)paramMap.get("siteId"));	
			boolean resultMark = (Boolean)resultMap.get("result");
			if(resultMark){
				//删除地点信息
				siteDao.delSite(paramMap);
				result.put("result", true);
			}else{
				result.put("result", false);
				result.put("failMsg", resultMap.get("failMsg"));
			}
			
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 查询全部地点
	 * @param Map<String,Object> paramMap  
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSites() throws Exception {

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try {
			resultList = siteDao.getSites();
		} catch (Exception e) {
			throw e;
		}
		return resultList;
	}
	
	/**
	 * @description 添加专业与教学地点的关联信息
	 * @param Map<String,Object> paramMap  地点名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public boolean addSiteReProfession(Map<String, Object> paramMap) throws Exception {

		boolean result = false;
		try {
			//删除这个专业的教师的关联信息
			siteDao.delSiteReProfession(paramMap);
			//添加关联信息
			siteDao.addSiteReProfession(paramMap);
			result = true;
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 获取某个专业与某个地点是不是关联信息（也可查询某个专业的教师地点信息）
	 * @param  专业id  地点id
	 * @return List
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSiteReProfession(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = siteDao.getSiteReProfession(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return list;
	}
	
	/**
	 * @description 地点管理列表  分页
	 * @param Map<String,Object> paramMap
	 * @return List 
	 * @throws Exception
	 */
	public Map<String,Object> getSiteList(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			List<Map<String, Object>> siteList = siteDao.getSiteList(paramMap);
			resultMap.put("siteList", siteList);
			resultMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = siteDao.getSiteList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			resultMap.put("totalPage", totalPage);
		}catch(Exception e){
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询某地点的详细信息  
	 * @param 地点id
	 * @return List 
	 * @throws Exception
	 */
	public Map<String,Object> getSiteDetail(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try{
			resultMap = siteDao.getSiteDetail(paramMap);
		}catch(Exception e){
			throw e;
		}
		return resultMap;
	}
	
}
